#coding=utf-8
#LightCam 统一开户视图
#init by hfh 20151030

#20141209,084829,trade,01,MAXZHITradeCnt,交易系统,股指成交峰值,3444270,,,

import multiprocessing,subprocess,sys
import socket,os,time,re,traceback,json,binascii
import lc_libs,pymongo


def genjson(filename,data):
	jdata={}
	jdata['rows']=data
	file=open(lchome+"/pyweb/static/json/uniclient/"+filename+".json",'w')
	file.write(json.dumps(jdata))
	file.close()


if __name__ == "__main__":
	
	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()


	#初始化数据库
	
	db=lc_libs.lc_mongodb_operater('127.0.0.1',27017)

	db.connect()
	Conn=db.getConn()
	if Conn==None:
		print "Can not Connect to the MondbDB"
		exit(0)
	
	dbname='lc_uniclient'
	psisdb=db.useDatabase(dbname)
	
	while True:
		try:
			
			cmds='''
				cd /monitor/LightCam/scripts/;
				LightCamPy lc_batch.py "unifront1|unieib1|unieib2|bizbip1|bizbip2" getsp /monitor/LightCam/temp/processstatus.json processstatus.json /monitor/LightCam/temp/;
				LightCamPy lc_batch.py "unifront1" get /monitor/LightCam/temp/loglist.json loglist.json /monitor/LightCam/pyweb/static/json/uniclient/;
				LightCamPy lc_batch.py "unifront1" get /monitor/LightCam/temp/loglistapi.json loglistapi.json /monitor/LightCam/pyweb/static/json/uniclient/;
				LightCamPy lc_batch.py "unifront1" get /monitor/LightCam/temp/logstate.json logstate.json /monitor/LightCam/pyweb/static/json/uniclient/
			'''
			
			#cmds="id"
			os.system(cmds)

			#业务状态
			thisfile=open("/monitor/LightCam/temp/appstatus.txt","rb")
			jsondata=[]
			for line in thisfile.readlines():
				if ',15,' in line:
					lines=line.split(',')
					thistime="%s-%s-%s %s:%s:%s" % (lines[0][0:4],lines[0][4:6],lines[0][6:8],lines[1][0:2],lines[1][2:4],lines[1][4:6])
					thisname=lines[6].decode('gbk').encode('utf8')
					thisvalue=lines[7]
					jsondata.append({'thistime':thistime,'thisname':thisname,'thisvalue':thisvalue})
			thisfile.close()
			genjson('appstatus',jsondata)



			#进程状态统计
			thisfile=open("/monitor/LightCam/temp/unifront1_processstatus.json","rb")
			datastr1=thisfile.read()
			thisfile.close()
			
			thisfile=open("/monitor/LightCam/temp/unieib1_processstatus.json","rb")
			datastr2=thisfile.read()
			thisfile.close()
			
			thisfile=open("/monitor/LightCam/temp/unieib2_processstatus.json","rb")
			datastr3=thisfile.read()
			thisfile.close()
			
			thisfile=open("/monitor/LightCam/temp/bizbip1_processstatus.json","rb")
			datastr4=thisfile.read()
			thisfile.close()
			
			thisfile=open("/monitor/LightCam/temp/bizbip2_processstatus.json","rb")
			datastr5=thisfile.read()
			thisfile.close()
			
			jsondata1=json.loads(datastr1)
			jsondata2=json.loads(datastr2)
			jsondata3=json.loads(datastr3)
			jsondata4=json.loads(datastr4)
			jsondata5=json.loads(datastr5)
			
			jsondata=jsondata1+jsondata2+jsondata3+jsondata4+jsondata5

			#print jsondata
			
			genjson('processstatus',jsondata)

			time.sleep(10)

		except Exception:
			excinfo=traceback.format_exc()
			logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
			logfile.write(logdate+" \n"+excinfo+"\n")
			logfile.flush()
			time.sleep(10)
			continue

